package com.tavio.vaultboy.element;

import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.tavio.vaultboy.element.type.ElementType;
import com.tavio.vaultboy.scene.SceneManager;
import com.tavio.vaultboy.util.vector.Vector;


public class Bullet extends RectangleElement {
	
	private Vector direction;
	
	private Vector currPos;
	
	private Element collidedElement = null;
	
	public Bullet(float x, float y, Vector direction, 
			VertexBufferObjectManager vertexBufferObjectManager) {
		super(ElementType.BULLET, x, y, 8, 8, vertexBufferObjectManager);
		currPos = new Vector(x, y);
		this.direction = direction;
	}

	@Override
	public void updatePositionAndCheckCollisions(float timeElapsed) {
		
		currPos = currPos.add(direction.multiply(7.5f));
		
		x = currPos.x;
		y = currPos.y;
		
		getBoundingBox().update();
		this.getRectangle().setPosition(x, y);
		
		//Check for collision with elements
		for(Element element : SceneManager.getInstance().collidableElements) {
			if(getBoundingBox().collidesWith(element.getBoundingBox()) && 
					! this.equals(element) && ! SceneManager.getInstance().getVaultBoy().equals(element)) {
				collidedElement = element;	
				
				if(element.elementType.equals(ElementType.BOX)) 
					element.accelerationModifier = element.accelerationModifier * -1;
				
				SceneManager.getInstance().removeBulletFromScene(this);
				
				break;
			}
		}
	}
		
}
